package com.qububu.db.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qububu.db.dao.SearchInfoDao;
import com.qububu.emporium.goods.dto.GoodsAboutDto.GoodsType;
import com.qububu.emporium.goods.dto.GoodsInfoDto;
import com.qububu.emporium.search.dto.HotSearchWordDto;
import com.qububu.utils.ParamConverter;

@Repository
public class SearchInfoDaoImpl implements SearchInfoDao{
	
	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource){
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public List<HotSearchWordDto> getSearchInfoByHotWord() {
		// TODO Auto-generated method stub
		
		String sql = "select * from hot_search";		
		List<HotSearchWordDto> searchList = null;
		searchList = new ArrayList<HotSearchWordDto>();
		
		List<Map<String, Object>> elemList = jdbcTemplate.queryForList(sql);
		HotSearchWordDto searchInfoDto = null;
		for (Map<String, Object> map : elemList) {
			searchInfoDto = new HotSearchWordDto(ParamConverter.convertToLongObj(String.valueOf(map.get("hot_id"))),  ParamConverter.convertToLongObj(String.valueOf((map.get("goods_info_id")))));

			searchInfoDto.setHotName(String.valueOf(map.get("hot_name")));
			
			searchList.add(searchInfoDto);			
		}
//		System.out.println("\n\n热词查询结果是--"+searchList.toString() +"\n\n");
		
		return searchList;
	}
	
	/**
	 * @功能 关键词（模糊）查找
	 * @备注 可能要放在goodsInfoDto内
	 */
	public List<GoodsInfoDto> getSearchInfoByKeyWord(String keyWord) {
		// TODO Auto-generated method stub
		StringBuffer sqlBuffer = new StringBuffer();
		sqlBuffer.append("select * from goods_info where goods_name LIKE ");
		sqlBuffer.append("'%");
		sqlBuffer.append(keyWord);
		sqlBuffer.append("%'");
		
		System.out.print("\n---"+sqlBuffer.toString()+"---\n");
		List<GoodsInfoDto> keyWordList = null;
		keyWordList = new ArrayList<GoodsInfoDto>();
		
		List<Map<String, Object>> elemList = jdbcTemplate.queryForList(String.valueOf(sqlBuffer));
		GoodsInfoDto goodsInfo = null;
		for (Map<String, Object> map : elemList) {
			goodsInfo = new GoodsInfoDto( (Long)(map.get("goods_id")));
			
			goodsInfo.setGoodsType(GoodsType.getType((Integer) map.get("goods_type")));
			goodsInfo.setGoodsDetailsUrl(String.valueOf(map.get("goods_details_url")));
			goodsInfo.setGoodsName(String.valueOf(map.get("goods_name")));
			goodsInfo.setGoodsPicUrl(String.valueOf(map.get("goods_pic_url")));
			goodsInfo.setGoodsPoint(ParamConverter.convertToInteger(String.valueOf(map.get(("goods_point")))));
			goodsInfo.setGoodsPrice(String.valueOf(map.get("goods_price")));
			goodsInfo.setGoodsPromise(String.valueOf(map.get("goods_promise")));
			goodsInfo.setGoodsSummaryPicUrls(String.valueOf(map.get("goods_summary_picUrls")));
			goodsInfo.setGoodsUseNum(ParamConverter.convertToInteger(String.valueOf(map.get(("goods_use_num")))));
			
			keyWordList.add(goodsInfo);
		}
		
		return keyWordList;
		
	}
}
